<?php
/**
 * 管理员控制器类
 */
namespace Admin\Controller;
use Think\Controller;
class AdminController extends CommonController  {
    // 管理员账户列表
    public function admin_list(){
        $admin_list = M('auth_group_access aga')->join('right join admin ad on ad.aid=aga.uid left join auth_group ag on aga.group_id = ag.id')->field('ad.aid,ad.aname,ad.email,ad.phone,ad.join_time,ad.available,ag.title')->select();
        $this->assign('admin_list', $admin_list);
        $this->assign('count', count($admin_list));
        $this->display();
    }
    // 添加管理员账号
    public function admin_add(){
        $post_data = I('post.data');
        if ($post_data != null) {
            $table = 'admin';
            $add['aname'] = $post_data['username'];
            $add['apwd'] = md5($post_data['pass']);
            $add['email'] = $post_data['email'];
            $add['phone'] = $post_data['phone'];
            $add['join_time'] = time();
            $add['available'] = 1;
            $info = $this->adddata($table, $add);
            if ($info['type'] == 1) {
                $agcd['uid'] = $info['id'];
                $agcd['group_id'] = $post_data['role'];
                M('auth_group_access')->add($agcd);
                unset($info['adddata']);
                unset($info['id']);
            }
            $this->ajaxReturn($info);
        } else {
            $auth_group = M('auth_group')->select();
            $this->assign('auth_group', $auth_group);
            $this->display();
        }
    }
    // 编辑管理员账号
    public function admin_edit()
    {
        $get_data = I('get.');
        $post_data = I('post.data');
        if ($get_data != null) {
            $list = M('auth_group_access aga')->join('right join admin ad on ad.aid=aga.uid left join auth_group ag on aga.group_id = ag.id')->field('ad.aid,ad.aname,ad.email,ad.phone,ad.join_time,ad.available,ag.id,ag.title')->where('ad.aid='.$get_data['id'])->find();
            $this->assign('list', $list);
            $auth_group = M('auth_group')->select();
            $this->assign('auth_group', $auth_group);
            $this->assign('key', $get_data['key']);
            $this->display();
        } elseif($post_data != null) {
            $table = 'admin';
            $old_data = M($table)->where('aid='.$post_data['id'])->find();
            $old_group = M('auth_group_access')->field('group_id')->where('uid='.$post_data['id'])->find();
            $edit['aname'] = $post_data['username'];
            $edit['apwd'] = md5($post_data['pass']);
            $edit['email'] = $post_data['email'];
            $edit['phone'] = $post_data['phone'];
            $where = 'aid='.$post_data['id'];
            $info = $this->editdata($table, $edit, $where);
            if ($info['type'] == 1) {
                if ($old_group['group_id'] != $post_data['role']) {
                    $agcd['uid'] = $post_data['id'];
                    $agcd['group_id'] = $post_data['role'];
                    $group = M('auth_group_access')->where('uid='.$post_data['id'])->setField($agcd);
                    if ($group) {
                        $old_data['old_group'] = $old_group;
                        $log_info = [0=>$old_data, 1=>$post_data];
                        $this->log('管理员账号', $log_info, 3);
                    }
                }
            }
            unset($info['editdata']['apwd']);
            $this->ajaxReturn($info);
        }
    }
    // 停用管理员账号
    public function admin_stop()
    {
        $post_data = I('post.data');
        $stop = M('admin')->where('aid='.$post_data)->setField('available', '0');
        if ($stop) {
            $info['id'] = $post_data;
            $info['type'] = 1;
            $info['info'] = '停用成功';
            $this->ajaxReturn($info);
        } else {
            $info['type'] = 0;
            $info['info'] = '停用失败';
            $this->ajaxReturn($info);
        }
    }
    // 启用管理员账号
    public function admin_start()
    {
        $post_data = I('post.data');
        $stop = M('admin')->where('aid='.$post_data)->setField('available', '1');
        if ($stop) {
            $info['id'] = $post_data;
            $info['type'] = 1;
            $info['info'] = '启用成功';
            $this->ajaxReturn($info);
        } else {
            $info['type'] = 0;
            $info['info'] = '启用失败';
            $this->ajaxReturn($info);
        }
    }
    // 删除管理员账号
    public function admin_del()
    {
        $get_post = I('post.del');
        $data = M('admin')->where('aid='.$get_post)->delete();
        if ($data) {
            $log_info = [0=>$get_post, 1=>$data];
            $this->log('管理员账号', $log_info, 4);
            $info['type'] = 1;
            $info['info'] = '删除成功';
        }else{
            $info['type'] = 0;
            $info['info'] = '删除失败';
        }
        $this->ajaxReturn($info);
    }
    // 管理员角色管理
    public function admin_role(){
        $ag = M('auth_group')->select();
        $agt = $ag;
        foreach ($ag as $agk => $agv) {
            $sql = 'select ac.ctitle from auth_rule ar left join auth_category ac on ar.category_id=ac.id where ar.id in('.$agv['rules'].') group by ac.ctitle';
            $ac = M()->query($sql);
            unset($ac[0]);
            foreach ($ac as $acv) {
                $acs .= $acv['ctitle'].',';
            }
            $ag[$agk]['rule_cate'] = rtrim($acs, ',');
            $acs = '';
            unset($ag[$agk]['rules']);
        }
        $this->assign('auth_group', $ag);
        $this->assign('count', count($ag));
        $this->display();
    }
    // 添加管理员角色管理
    public function role_add()
    {
        $post_data = I('post.data');
        if ($post_data != null) {
            $old_data = 0;
            $table = 'auth_group';
            $add['title'] = $post_data['name'];
            $rules = implode(',', $post_data['rules']);
            $add['rules'] = $rules;
            $add['status'] = 1;
            $list = $this->adddata($table, $add);
            $log_info = [0=>$old_data, 1=>$add];
            $this->log('管理员角色管理', $log_info, 2);
            $this->ajaxReturn($list);
        } else {
            // 查询所有权限分类
            $sql = 'select ac.ctitle from auth_rule ar left join auth_category ac on ar.category_id=ac.id where ar.id group by ac.ctitle';
            $ac = M()->query($sql);
            unset($ac[0]);
            // 查询所有角色权限
            $sql = 'select ac.ctitle,ar.id,ar.title from auth_rule ar left join auth_category ac on ar.category_id=ac.id where ar.second_dir>-1';
            $ar = M()->query($sql);
            foreach ($ac as $ack => $acv) {
                foreach ($ar as $ark => $arv) {
                    if ($arv['ctitle'] == $acv['ctitle']) {
                        $ac[$ack]['roles'][$ark]['role_id'] = $arv['id'];
                        $ac[$ack]['roles'][$ark]['title'] = $arv['title'];
                    }
                }
            }
            $this->assign('ac', $ac);
            $this->display();
        }
    }
    // 编辑管理员角色管理
    public function role_edit()
    {
        $get_data = I('get.');
        $post_data = I('post.data');
        if ($get_data != null) {
            $list = M('auth_group')->where('id='.$get_data['id'])->find();
            // 查询所有权限分类
            $sql = 'select ac.ctitle from auth_rule ar left join auth_category ac on ar.category_id=ac.id group by ac.ctitle';
            $ac = M()->query($sql);
            unset($ac[0]);
            // 查询所有角色权限
            $sql = 'select ac.ctitle,ar.id,ar.title from auth_rule ar left join auth_category ac on ar.category_id=ac.id where ar.second_dir>-1';
            $ar = M()->query($sql);
            // 查询角色存在的权限
            $sql = 'select ar.id from auth_rule ar left join auth_category ac on ar.category_id=ac.id where ar.second_dir>-1 and ar.id in('.$list['rules'].')';
            $arc = M()->query($sql);
            foreach ($arc as $arck => $arcv) {
                $arct[] = $arcv['id'];
            }
            foreach ($ac as $ack => $acv) {
                foreach ($ar as $ark => $arv) {
                    if ($arv['ctitle'] == $acv['ctitle']) {
                        $ac[$ack]['roles'][$ark]['role_id'] = $arv['id'];
                        $ac[$ack]['roles'][$ark]['title'] = $arv['title'];
                    }
                }
            }
            $arc = $arct;
            $list['rule_cate'] = $ac;
            unset($list['rules']);
            unset($list['status']);
            $this->assign('list', $list);
            $this->assign('key', $get_data['key']);
            $this->assign('arc', $arc);
            $this->display();
        } elseif ($post_data != null) {
            $table = 'auth_group';
            $old_data = M($table)->where('id='.$post_data['id'])->find();
            foreach ($post_data['rules'] as $rv) {
                $rules .= $rv.',';
            }
            $rules .= '14,15';
            $edit['rules'] = $rules;
            $edit['title'] = $post_data['name'];
            $edit['status'] = 1;
            $where = 'id='.$post_data['id'];
            $info = $this->editdata($table, $edit, $where);
            if ($info['type'] == 1) {
                $sql = 'select ac.ctitle from auth_rule ar left join auth_category ac on ar.category_id=ac.id where ar.id in('.$info['editdata']['rules'].') group by ac.ctitle';
                $ac = M()->query($sql);
                unset($ac[0]);
                foreach ($ac as $acv) {
                    $act .= $acv['ctitle'].',';
                }
                $info['editdata']['rules'] = rtrim($act, ',');
                $info['key'] = $post_data['key'];
                $log_info = [0=>$old_data, 1=>$edit];
                $this->log('管理员角色管理', $log_info, 3);
            }
            $this->ajaxReturn($info);
        }
    }
    // 删除管理员角色管理
    public function role_del()
    {
        $get_post = I('post.del');
        $table = 'auth_group';
        $data = $this->deldata($table, $get_post);
        if ($data) {
            $log_info = [0=>$get_post, 1=>$data];
            $this->log('管理员角色管理', $log_info, 4);
            $info['type'] = 1;
            $info['info'] = '删除成功';
        } else {
            $info['type'] = 0;
            $info['info'] = '删除失败';
        }
        $this->ajaxReturn($info);
    }
    // 管理员权限分类
    public function admin_cate(){
        $auth_category = M('auth_category')->select();
        $this->assign('auth_category', $auth_category);
        $this->assign('count', count($auth_category));
        $this->display();
    }
    // 添加管理员权限分类
    public function cate_add()
    {
        $post_data = I('post.data');
        $table = 'auth_category';
        $add['ctitle'] = $post_data['ctitle'];
        $old_data = M('auth_category')->where('id='.$post_data['id'])->find();
        $list = $this->adddata($table, $add);
        if ($list) {
            $list['url'] = U('admin/admin/cate_edit', array('id'=>$list['id'],'key'=>0));
            $info['type'] = 1;
            $info['data'] = $list;
            $log_info = [0=>$old_data, 1=>$post_data];
            $this->log('管理员权限分类', $log_info, 2);
        } else {
            $info['type'] = 0;
        }
        $this->ajaxReturn($info);
    }
    // 编辑管理员权限分类
    public function cate_edit()
    {
        $get_data = I('get.');
        $post_data = I('post.data');
        if ($get_data != null) {
            $list = M('auth_category')->where('id='.$get_data['id'])->find();
            $this->assign('id', $list['id']);
            $this->assign('ctitle', $list['ctitle']);
            $this->assign('key', $get_data['key']);
            $this->display();
        } elseif($post_data != null) {
            $table = 'auth_category';
            $old_data = M('auth_category')->where('id='.$post_data['id'])->find();
            $edit = ['ctitle'=>$post_data['ctitle']];
            $where = 'id='.$post_data['id'];
            $info = $this->editdata($table, $edit, $where);
            $info['key'] = $post_data['key'];
            $log_info = [0=>$old_data, 1=>$post_data];
            $this->log('管理员权限规则', $log_info, 3);
            $this->ajaxReturn($info);
        }
    }
    // 删除管理员权限分类
    public function cate_del()
    {
        $get_post = I('post.del');
        $table = 'auth_category';
        $data = $this->deldata($table, $get_post);
        if ($data) {
            $log_info = [0=>$get_post, 1=>$data];
            $this->log('管理员权限分类', $log_info, 4);
            $info['type'] = 1;
            $info['info'] = '删除成功';
        }else{
            $info['type'] = 0;
            $info['info'] = '删除失败';
        }
        $this->ajaxReturn($info);
    }
    // 管理员权限规则
    public function admin_rule(){
        $auth_rule = M()->table(array('auth_category'=>'ac','auth_rule'=>'ar'))->where('ar.category_id=ac.id and ar.second_dir=1')->field('ar.id,ar.name,ar.title,ac.ctitle')->select();
        $auth_category = M('auth_category')->field('id,ctitle')->select();
        $this->assign('auth_rule', $auth_rule);
        $this->assign('auth_category', $auth_category);
        $this->assign('count', count($auth_rule));
        $this->display();
    }
    // 添加员权限规则
    public function rule_add()
    {
        $post_data = I('post.data');
        $data['name'] = $post_data['rule'];
        $data['title'] = $post_data['name'];
        $data['category_id'] = $post_data['cname'];
        $add = M('auth_rule')->add($data);
        if ($add) {
            $add_list = M()->table('auth_rule ar,auth_category ac')->field('ar.id,ar.name,ar.title,ac.ctitle')->where('ar.category_id=ac.id and ar.id='.$add)->find();
            $add_list['url'] = U('admin/admin/rule_edit', array('id'=>$add_list['id'],'key'=>0));
            $log_info = [0=>$post_data, 1=>$add_list];
            $this->log('管理员权限规则', $log_info, 2);
            $data_back = ['type'=>1,'info'=>'增加成功','data'=>$add_list];
        } else {
            $data_back = ['type'=>0,'info'=>'修改失败'];
        }
        $this->ajaxReturn($data_back);
    }
    // 编辑管理员权限规则
    public function rule_edit(){
        $get_data = I('get.');
        $post_data = I('post.data');
        if ($get_data != null) {
            $auth_rule = M()->table(array('auth_category'=>'ac','auth_rule'=>'ar'))->where('ar.category_id=ac.id and ar.id='.$get_data['id'])->field('ar.id,ar.name,ar.title,ac.ctitle')->find();
            $this->assign('auth_rule', $auth_rule);
            $this->assign('key', $get_data['key']);
            $this->display();
        } elseif($post_data != null) {
            $data['name'] = $post_data['name'];
            $data['title'] = $post_data['title'];
            $old_auth_rule = M('auth_rule ar')->where('id='.$post_data['id'])->field('ar.id,ar.name,ar.title')->find();
            $auth_rule = M('auth_rule')->where('id='.$post_data['id'])->setField($data);
            if ($auth_rule) {
                $data_back = ['type'=>1,'info'=>'修改成功','data'=>$post_data];
                $log_info = [0=>$old_auth_rule, 1=>$post_data];
                $this->log('管理员权限规则', $log_info, 3);
            } else {
                $data_back = ['type'=>0,'info'=>'修改失败'];
            }
            $this->ajaxReturn($data_back);
        }
    }
    // 删除管理员权限规则
    public function rule_del()
    {
        $get_post = I('post.del');
        $table = 'auth_rule';
        $data = $this->deldata($table, $get_post);
        if ($data) {
            $log_info = [0=>$get_post, 1=>$data];
            $this->log('管理员权限规则', $log_info, 4);
            $info['type'] = 1;
            $info['info'] = '删除成功';
        }else{
            $info['type'] = 0;
            $info['info'] = '删除失败';
        }
        $this->ajaxReturn($info);
    }
}